<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Modules\DataCheck\Enums\TaskStateEnum;
use Modules\DataCheck\Excel\Export\DcTaskExport;
use Modules\DataCheck\Models\DcTask;
use Modules\DataCheck\Services\DataCheckService;
use Modules\DataCheck\Services\Import\SourceFileImportService;
use Symfony\Component\Console\Command\Command as CommandAlias;

class DataCheckExport extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'data-check-export {taskId}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '数据核对导出';

    /**
     * Execute the console command.
     *
     * @param DcTaskExport $export
     * @return int
     */
    public function handle(DcTaskExport $export): int
    {
        $taskId = $this->argument('taskId');

        //获取任务
        $task = DcTask::query()->where('task_id', $taskId)->first();
        if (empty($task)) {
            $this->error('任务不存在');
            return CommandAlias::FAILURE;
        }

        if ($task->task_state != TaskStateEnum::COMPLETE->value) {
            $this->error('数据未核对完成，不可导出');
            return CommandAlias::FAILURE;
        }

        $result = $export->handle($task);

        $this->info('数据导出成功');
        $this->info('保存路径：' . $result['real_path']);
        $this->info('下载地址：' . $result['url']);

        return Command::SUCCESS;
    }
}
